package com.ruoyi.web.controller.lowcode;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.utils.redis.CacheUtils;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lowcode.domain.vo.LcDictionaryVo;
import com.ruoyi.lowcode.domain.bo.LcDictionaryBo;
import com.ruoyi.lowcode.service.ILcDictionaryService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据字典
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lowcode/dictionary")
public class LcDictionaryController extends BaseController {

    private final ILcDictionaryService iLcDictionaryService;

    /**
     * 查询数据字典列表
     */
    @SaCheckPermission("lowcode:dictionary:list")
    @GetMapping("/list")
    public TableDataInfo<LcDictionaryVo> list(LcDictionaryBo bo, PageQuery pageQuery) {
        return iLcDictionaryService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取全部字典
     *
     * @return
     */
    @GetMapping("/queryList")
    public R<List<LcDictionaryVo>> queryList(LcDictionaryBo bo){
        List<LcDictionaryVo> dictionaryVoList = CacheUtils.get(CacheNames.LOW_CODE, "dictionary_queryList_all");
        if(ObjectUtil.isNull(dictionaryVoList) || dictionaryVoList.size() == 0){
            dictionaryVoList = iLcDictionaryService.queryList(bo);
            CacheUtils.put(CacheNames.LOW_CODE, "dictionary_queryList_all", dictionaryVoList);
        }
        return R.ok(dictionaryVoList);
    }

    /**
     * 导出数据字典列表
     */
//    @SaCheckPermission("lowcode:dictionary:export")
//    @Log(title = "数据字典", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(LcDictionaryBo bo, HttpServletResponse response) {
//        List<LcDictionaryVo> list = iLcDictionaryService.queryList(bo);
//        ExcelUtil.exportExcel(list, "数据字典", LcDictionaryVo.class, response);
//    }

    /**
     * 获取数据字典详细信息
     *
     * @param dictionaryId 主键
     */
    @SaCheckPermission("lowcode:dictionary:query")
    @GetMapping("/{dictionaryId}")
    public R<LcDictionaryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dictionaryId) {
        return R.ok(iLcDictionaryService.queryById(dictionaryId));
    }

    /**
     * 新增数据字典
     */
    @SaCheckPermission("lowcode:dictionary:add")
    @Log(title = "数据字典", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LcDictionaryBo bo) {
        return toAjax(iLcDictionaryService.insertByBo(bo));
    }

    /**
     * 修改数据字典
     */
    @SaCheckPermission("lowcode:dictionary:edit")
    @Log(title = "数据字典", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LcDictionaryBo bo) {
        return toAjax(iLcDictionaryService.updateByBo(bo));
    }

    /**
     * 删除数据字典
     *
     * @param dictionaryIds 主键串
     */
    @SaCheckPermission("lowcode:dictionary:remove")
    @Log(title = "数据字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictionaryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dictionaryIds) {
        return toAjax(iLcDictionaryService.deleteWithValidByIds(Arrays.asList(dictionaryIds), true));
    }
}
